package com.wonyesoft.wuliu;

import java.util.LinkedList;
import java.util.List;

import tools.CrashHandler;
import javabeen.UserInfoBeen;
import android.R.bool;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Vibrator;
import android.util.Log;
import android.widget.Toast;
import cn.jpush.android.api.JPushInterface;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;
import com.lidroid.xutils.util.PreferencesCookieStore;

public class AppContent extends Application {

	public static final int LOCATION_TIME = 30000;
	public LocationClient mLocationClient;
	public GeofenceClient mGeofenceClient;
	public MyLocationListener mMyLocationListener;
	public Vibrator mVibrator;
	private static Context sContext;
	public String adrr = "点击获取当前位置";
	public double longitude = 0.0;
	public double latitude = 0.0;
	public BDLocation localLocation = null;
	//
	public GetPushMessage callback;
	// 用户ID
	public String userId;
	// 订单是否是当前显示
	public boolean isOrderActivity = false;
	// 订单列表是否需要更改
	public boolean OrderActivity_data_has_change = false;
	// 订单列表刷新那一个
	public String OrderActivity_data_change_poition;
	// 订单ID
	public String OrderId;

	private List<Activity> activityList=new LinkedList<Activity>();
	
	
	// 消息推送channelID
	public String channelId;

	/**
	 * 订单语音声音
	 */
	public SoundPool ordermessagePool;
	public int ordermessage_id;
	public int soud_id;
	/**
	 * 新订单声音
	 */
	public SoundPool soundPool;
	private boolean SounComplete = false;

	
	/**
	 * 订单新的消息声音
	 */
	public SoundPool order_new_info_Pool;
	public int order_new_info_id;
	public boolean order_new_info_flag;
	/**
	 * 订单完成声音
	 */
	public SoundPool order_complete_Pool;
	public int order_complete_id;
	public boolean order_complete_Pool_flag;
	/**
	 * 订单取消声音
	 */
	public SoundPool order_remove_Pool;
	public int order_remove_Pool_id;
	public boolean order_remove_Pool_flag;

	
	
	
	boolean ordermessageComplete = false;
	// 消息推送的uid
	public String messagepushuserId="null";

	public boolean New_Order_Push = false;

	// 消息USERid和ChanlID的接口
	public GetBaiDuPush getBaiDuPush;
	public UserInfoBeen userinfobeen;

	/**
	 * 获得gps的地址
	 */
	public getGpsAddress gpsAddress;
	
	public static Context getContext() {
		return sContext;
	}

	public static PreferencesCookieStore preferencesCookieStore;

	public static PreferencesCookieStore getPreferencesCookieStore() {
		return preferencesCookieStore;
	}

	public static void setPreferencesCookieStore(
			PreferencesCookieStore preferencesCookieStore) {
		AppContent.preferencesCookieStore = preferencesCookieStore;
	}

	public void onCreate() {
		super.onCreate(); // 在使用 SDK 各组间之前初始化 context信息，传入ApplicationContext
	//	FrontiaApplication.initFrontiaApplication(getApplicationContext());
		sContext = getApplicationContext();
		// 保存报错信息
	//	 CrashHandler crashHandler = CrashHandler.getInstance();
	//	 crashHandler.init(getApplicationContext());
		 JPushInterface.setDebugMode(true); 	// 设置开启日志,发布时请关闭日志
		 JPushInterface.init(getApplicationContext());
		 
		mLocationClient = new LocationClient(this.getApplicationContext());
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		mGeofenceClient = new GeofenceClient(getApplicationContext());
		mVibrator = (Vibrator) getApplicationContext().getSystemService(
				Service.VIBRATOR_SERVICE);
		// SDKInitializer.initialize(sContext);
		IniSound();
		this.channelId=JPushInterface.getRegistrationID(getApplicationContext());
	}
	
	
	public void getJPushID(){
		this.channelId=JPushInterface.getRegistrationID(getApplicationContext());
	}

	public void addActivity(Activity activity){
		activityList.add(activity);
	}
	
	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		super.onTerminate();
		for (Activity activity : activityList) {
			if (activity!=null) {
				activity.finish();
			}
			
		}
		System.exit(0);
	}
	
	@SuppressLint("NewApi")
	public void IniSound() {
		soundPool = new SoundPool(2, AudioManager.STREAM_RING, 0);
		soud_id = soundPool.load(this, R.raw.mssage, 1);
		soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

			@Override
			public void onLoadComplete(SoundPool sound, int arg1, int arg2) {
				// TODO Auto-generated method stub
				SounComplete = true;
			}
		});
		ordermessagePool = new SoundPool(2, AudioManager.STREAM_RING, 0);
		ordermessage_id = ordermessagePool.load(this, R.raw.ordernewmessage, 1);
		ordermessagePool
				.setOnLoadCompleteListener(new OnLoadCompleteListener() {

					@Override
					public void onLoadComplete(SoundPool arg0, int arg1,
							int arg2) {
						// TODO Auto-generated method stub
						Log.v("出来", "onLoadComplete");
						ordermessageComplete = true;
					}
				});

		
		//-*
		order_new_info_Pool = new SoundPool(2, AudioManager.STREAM_RING, 0);
		order_new_info_id = order_new_info_Pool.load(this, R.raw.ordernewinfo, 1);
		order_new_info_Pool
				.setOnLoadCompleteListener(new OnLoadCompleteListener() {

					@Override
					public void onLoadComplete(SoundPool arg0, int arg1,
							int arg2) {
						// TODO Auto-generated method stub
						//Log.v("出来", "onLoadComplete");
						order_new_info_flag = true;
					}
				});
		//-*
		order_complete_Pool = new SoundPool(2, AudioManager.STREAM_RING, 0);
		order_complete_id = order_complete_Pool.load(this, R.raw.ordercomplete, 1);
		order_complete_Pool
				.setOnLoadCompleteListener(new OnLoadCompleteListener() {

					@Override
					public void onLoadComplete(SoundPool arg0, int arg1,
							int arg2) {
						// TODO Auto-generated method stub
						//Log.v("出来", "onLoadComplete");
						order_complete_Pool_flag = true;
					}
				});
		//-*
		order_remove_Pool = new SoundPool(2, AudioManager.STREAM_RING, 0);
		order_remove_Pool_id = order_remove_Pool.load(this, R.raw.orderremovebydriver, 1);
		order_remove_Pool
				.setOnLoadCompleteListener(new OnLoadCompleteListener() {

					@Override
					public void onLoadComplete(SoundPool arg0, int arg1,
							int arg2) {
						// TODO Auto-generated method stub
					//	Log.v("出来", "onLoadComplete");
						order_remove_Pool_flag = true;
					}
				});

		
	}

	/**
	 * 播放声音
	 */
	public void PlaySound(int type) {
		if (type == 1) {

			if (SounComplete) {
				AudioManager amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				float audioMaxVolum = amAudioManager
						.getStreamMaxVolume(AudioManager.STREAM_RING);
				Log.v("----------------", "--------------------------");
				Log.v("audioMaxVolum", "" + audioMaxVolum);
				Log.v("----------------", "--------------------------");
				soundPool.play(soud_id, audioMaxVolum, audioMaxVolum, 0, 0, 1);
			} else {
				Toast.makeText(this, "音乐控件还未准备好！！！2秒就好", Toast.LENGTH_LONG)
						.show();
			}
		}
		if (type == 2) {
			if (ordermessageComplete) {
				AudioManager amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				float audioMaxVolum = amAudioManager
						.getStreamMaxVolume(AudioManager.STREAM_RING);
				Log.v("----------------", "--------------------------");
				Log.v("audioMaxVolum", "" + audioMaxVolum);
				Log.v("----------------", "--------------------------");
				ordermessagePool.play(ordermessage_id, audioMaxVolum,
						audioMaxVolum, 0, 0, 1);
			} else {
				Toast.makeText(this, "音乐控件还未准备好！！！2秒就好", Toast.LENGTH_SHORT)
						.show();
			}
		}
		if (type == 3) {
			if (order_new_info_flag) {
				AudioManager amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				float audioMaxVolum = amAudioManager
						.getStreamMaxVolume(AudioManager.STREAM_RING);
				Log.v("----------------", "--------------------------");
				Log.v("audioMaxVolum", "" + audioMaxVolum);
				Log.v("----------------", "--------------------------");
				order_new_info_Pool.play(order_new_info_id, audioMaxVolum,
						audioMaxVolum, 0, 0, 1);
			} else {
				Toast.makeText(this, "音乐控件还未准备好！！！2秒就好", Toast.LENGTH_SHORT)
						.show();
			}
		}
		if (type == 4) {
			if (order_complete_Pool_flag) {
				AudioManager amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				float audioMaxVolum = amAudioManager
						.getStreamMaxVolume(AudioManager.STREAM_RING);
				Log.v("----------------", "--------------------------");
				Log.v("audioMaxVolum", "" + audioMaxVolum);
				Log.v("----------------", "--------------------------");
				order_complete_Pool.play(order_complete_id, audioMaxVolum,
						audioMaxVolum, 0, 0, 1);
			} else {
				Toast.makeText(this, "音乐控件还未准备好！！！2秒就好", Toast.LENGTH_SHORT)
						.show();
			}
		}
		if (type == 5) {
			if (order_remove_Pool_flag) {
				AudioManager amAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
				float audioMaxVolum = amAudioManager
						.getStreamMaxVolume(AudioManager.STREAM_RING);
				Log.v("----------------", "--------------------------");
				Log.v("audioMaxVolum", "" + audioMaxVolum);
				Log.v("----------------", "--------------------------");
				order_remove_Pool.play(order_remove_Pool_id, audioMaxVolum,
						audioMaxVolum, 0, 0, 1);
			} else {
				Toast.makeText(this, "音乐控件还未准备好！！！2秒就好", Toast.LENGTH_SHORT)
						.show();
			}
		}
		
	}

	/**
	 * 实现实位回调监听
	 */
	public class MyLocationListener implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			localLocation = location;
			// Receive Location
			adrr = location.getAddrStr();
			if (gpsAddress!=null) {
				gpsAddress.getlocation(adrr);
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			latitude = location.getLatitude();
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			longitude = location.getLongitude();
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
				sb.append("\ndirection : ");
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				sb.append(location.getDirection());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				// 运营商信息
				sb.append("\noperationers : ");
				sb.append(location.getOperators());
			}
			// logMsg(sb.toString());
			// Log.i("BaiduLocationApiDem", sb.toString());
		}

	}

	/**
	 * 显示请求字符串
	 * 
	 * @param str
	 */
	public void logMsg(String str) {
		try {
			// if (mLocationResult != null)
			// mLocationResult.setText(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 高精度地理围栏回调
	 * 
	 * @author jpren
	 * 
	 */

	/** ----------------------消息推送-------------------------------- */

	public void SetPushMessage(boolean flag, String msg, String CatTeamLeader,
			String CatTeamId) {
		if (flag) {
			// pusHandler.sendMessage(msg);
			// pusHandler.sendEmptyMessage(1);
			if (callback != null) {
				Log.v("直接运行了------------", "----------------");
				// new_home_activity_data_has_change = true;
				OrderActivity_data_has_change = true;
				OrderActivity_data_change_poition = "1";
				callback.GetMessage(msg, CatTeamLeader, CatTeamId);
			} else {
				Log.v("callback", "空");
			}

		}
	}

	// 消息接口
	public interface GetPushMessage {
		public void GetMessage(String msg, String CarTeamLeader,
				String CarTeamId);
	}

	// 消息接口设置
	public void SetPushMessageLister(GetPushMessage call) {
		this.callback = call;
	}

	// 百度推送开启后开始写入
	public void InPutPushMessage(String channelId, String messagepushuserId) {
		this.channelId = channelId;
	//	this.messagepushuserId = messagepushuserId;
		if (getBaiDuPush != null) {
			getBaiDuPush.GetMessage(channelId, messagepushuserId);
		}
	}

	// 消息推送获得userid和channelId的接口
	public interface GetBaiDuPush {
		public void GetMessage(String channelId, String userId);
	}

	// 设置GetBaiDuPush的监听
	public void SetGetBaiDuPush(GetBaiDuPush getBaiDuPush) {
		Log.v("", "到这里");
		this.getBaiDuPush = getBaiDuPush;
		this.channelId=JPushInterface.getRegistrationID(getApplicationContext());
	}

	/**
	 * 
	 * @author luck
	 *获得gps的地址
	 */
	public interface getGpsAddress{
		public void getlocation(String location);
	}
	
	public void setGpsCallback(getGpsAddress gpsAddress){
		this.gpsAddress=gpsAddress;
	}
	

	
	
}